home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / Make / source / remake.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-27  |  31.8 KB  |  1,242 lines

  1. /* Basic dependency engine for GNU Make.
  2. Copyright (C) 1988,89,90,91,92,93,94,95,96,97 Free Software Foundation, Inc.
  3. This file is part of GNU Make.
  4.  
  5. GNU Make is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. GNU Make is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU Make; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "make.h"
  20. #include "filedef.h"
  21. #include "job.h"
  22. #include "commands.h"
  23. #include "dep.h"
  24. #include <assert.h>
  25.  
  26. #ifdef HAVE_FCNTL_H
  27. #include <fcntl.h>
  28. #else
  29. #include <sys/file.h>
  30. #endif
  31.  
  32. #ifdef  __MSDOS__
  33. #include "variable.h"
  34. #endif
  35.  
  36. #ifdef VMS
  37. #include <starlet.h>
  38. #endif
  39. #ifdef WINDOWS32
  40. #include <io.h>
  41. #endif
  42.  
  43. extern int try_implicit_rule PARAMS ((struct file *file, unsigned int depth));
  44.  
  45.  
  46. /* Incremented when a command is started (under -n, when one would be).  */
  47. unsigned int commands_started = 0;
  48.  
  49. static int update_file PARAMS ((struct file *file, unsigned int depth));
  50. static int update_file_1 PARAMS ((struct file *file, unsigned int depth));
  51. static int check_dep PARAMS ((struct file *file, unsigned int depth, time_t this_mtime, int *must_make_ptr));
  52. static int touch_file PARAMS ((struct file *file));
  53. static void remake_file PARAMS ((struct file *file));
  54. static time_t name_mtime PARAMS ((char *name));
  55. static int library_search PARAMS ((char **lib, time_t *mtime_ptr));
  56.  
  57. extern time_t f_mtime PARAMS ((struct file *file, int search));
  58.  
  59.  
  60. /* Remake all the goals in the `struct dep' chain GOALS.  Return -1 if nothing
  61.    was done, 0 if all goals were updated successfully, or 1 if a goal failed.
  62.    If MAKEFILES is nonzero, these goals are makefiles, so -t, -q, and -n should
  63.    be disabled for them unless they were also command-line targets, and we
  64.    should only make one goal at a time and return as soon as one goal whose
  65.    `changed' member is nonzero is successfully made.  */
  66.  
  67. int
  68. update_goal_chain (goals, makefiles)
  69.      register struct dep *goals;
  70.      int makefiles;
  71. {
  72.   int t = touch_flag, q = question_flag, n = just_print_flag;
  73.   unsigned int j = job_slots;
  74.   int status = -1;
  75.  
  76. #define    MTIME(file) (makefiles ? file_mtime_no_search (file) \
  77.              : file_mtime (file))
  78.  
  79.   /* Duplicate the chain so we can remove things from it.  */
  80.  
  81.   goals = copy_dep_chain (goals);
  82.  
  83.   {
  84.     /* Clear the `changed' flag of each goal in the chain.
  85.        We will use the flag below to notice when any commands
  86.        have actually been run for a target.  When no commands
  87.        have been run, we give an "up to date" diagnostic.  */
  88.  
  89.     struct dep *g;
  90.     for (g = goals; g != 0; g = g->next)
  91.       g->changed = 0;
  92.   }
  93.  
  94.   if (makefiles)
  95.     /* Only run one job at a time.  */
  96.     job_slots = 1;
  97.  
  98.   /* Update all the goals until they are all finished.  */
  99.  
  100.   while (goals != 0)
  101.     {
  102.       register struct dep *g, *lastgoal;
  103.  
  104.       /* Start jobs that are waiting for the load to go down.  */
  105.  
  106.       start_waiting_jobs ();
  107.  
  108.       /* Wait for a child to die.  */
  109.  
  110.       reap_children (1, 0);
  111.  
  112.       lastgoal = 0;
  113.       g = goals;
  114.       while (g != 0)
  115.     {
  116.       /* Iterate over all double-colon entries for this file.  */
  117.       struct file *file = g->file;
  118.       int stop, any_not_updated = 0;
  119.  
  120.       for (file = g->file->double_colon ? g->file->double_colon : g->file;
  121.            file != NULL;
  122.            file = file->prev)
  123.         {
  124.           unsigned int ocommands_started;
  125.           int x;
  126.           time_t mtime = MTIME (file);
  127.           check_renamed (file);
  128.           if (makefiles)
  129.         {
  130.           if (file->cmd_target)
  131.             {
  132.               touch_flag = t;
  133.               question_flag = q;
  134.               just_print_flag = n;
  135.             }
  136.           else
  137.             touch_flag = question_flag = just_print_flag = 0;
  138.         }
  139.  
  140.           /* Save the old value of `commands_started' so we can compare
  141.          later.  It will be incremented when any commands are
  142.          actually run.  */
  143.           ocommands_started = commands_started;
  144.  
  145.           x = update_file (file, makefiles ? 1 : 0);
  146.           check_renamed (file);
  147.  
  148.           /* Set the goal's `changed' flag if any commands were started
  149.          by calling update_file above.  We check this flag below to
  150.          decide when to give an "up to date" diagnostic.  */
  151.           g->changed += commands_started - ocommands_started;
  152.  
  153.           stop = 0;
  154.           if (x != 0 || file->updated)
  155.         {
  156.           /* If STATUS was not already 1, set it to 1 if
  157.              updating failed, or to 0 if updating succeeded.
  158.              Leave STATUS as it is if no updating was done.  */
  159.  
  160.           if (status < 1)
  161.             {
  162.               if (file->update_status != 0)
  163.             {
  164.               /* Updating failed, or -q triggered.
  165.                  The STATUS value tells our caller which.  */
  166.               status = file->update_status;
  167.               /* If -q just triggered, stop immediately.
  168.                  It doesn't matter how much more we run,
  169.                  since we already know the answer to return.  */
  170.               stop = (!keep_going_flag && !question_flag
  171.                   && !makefiles);
  172.             }
  173.               else if (MTIME (file) != mtime)
  174.             {
  175.               /* Updating was done.  If this is a makefile and
  176.                  just_print_flag or question_flag is set
  177.                  (meaning -n or -q was given and this file was
  178.                  specified as a command-line target), don't
  179.                  change STATUS.  If STATUS is changed, we will
  180.                  get re-exec'd, and fall into an infinite loop.  */
  181.               if (!makefiles
  182.                   || (!just_print_flag && !question_flag))
  183.                 status = 0;
  184.               if (makefiles && file->dontcare)
  185.                 /* This is a default makefile.  Stop remaking.  */
  186.                 stop = 1;
  187.             }
  188.             }
  189.         }
  190.  
  191.           /* Keep track if any double-colon entry is not finished.
  192.                  When they are all finished, the goal is finished.  */
  193.           any_not_updated |= !file->updated;
  194.  
  195.           if (stop)
  196.         break;
  197.         }
  198.  
  199.       /* Reset FILE since it is null at the end of the loop.  */
  200.       file = g->file;
  201.  
  202.       if (stop || !any_not_updated)
  203.         {
  204.           /* If we have found nothing whatever to do for the goal,
  205.          print a message saying nothing needs doing.  */
  206.  
  207.           if (!makefiles
  208.           /* If the update_status is zero, we updated successfully
  209.              or not at all.  G->changed will have been set above if
  210.              any commands were actually started for this goal.  */
  211.           && file->update_status == 0 && !g->changed
  212.           /* Never give a message under -s or -q.  */
  213.           && !silent_flag && !question_flag)
  214.         message (1, ((file->phony || file->cmds == 0)
  215.                  ? "Nothing to be done for `%s'."
  216.                  : "`%s' is up to date."),
  217.              file->name);
  218.  
  219.           /* This goal is finished.  Remove it from the chain.  */
  220.           if (lastgoal == 0)
  221.         goals = g->next;
  222.           else
  223.         lastgoal->next = g->next;
  224.  
  225.           /* Free the storage.  */
  226.           free ((char *) g);
  227.  
  228.           g = lastgoal == 0 ? goals : lastgoal->next;
  229.  
  230.           if (stop)
  231.         break;
  232.         }
  233.       else
  234.         {
  235.           lastgoal = g;
  236.           g = g->next;
  237.         }
  238.     }
  239.     }
  240.  
  241.   if (makefiles)
  242.     {
  243.       touch_flag = t;
  244.       question_flag = q;
  245.       just_print_flag = n;
  246.       job_slots = j;
  247.     }
  248.   return status;
  249. }
  250.  
  251. /* If FILE is not up to date, execute the commands for it.
  252.    Return 0 if successful, 1 if unsuccessful;
  253.    but with some flag settings, just call `exit' if unsuccessful.
  254.  
  255.    DEPTH is the depth in recursions of this function.
  256.    We increment it during the consideration of our dependencies,
  257.    then decrement it again after finding out whether this file
  258.    is out of date.
  259.  
  260.    If there are multiple double-colon entries for FILE,
  261.    each is considered in turn.  */
  262.  
  263. static int
  264. update_file (file, depth)
  265.      struct file *file;
  266.      unsigned int depth;
  267. {
  268.   register int status = 0;
  269.   register struct file *f;
  270.  
  271.   for (f = file->double_colon ? file->double_colon : file; f != 0; f = f->prev)
  272.     {
  273.       status |= update_file_1 (f, depth);
  274.       check_renamed (f);
  275.  
  276.       if (status != 0 && !keep_going_flag)
  277.     return status;
  278.  
  279.       switch (f->command_state)
  280.     {
  281.     case cs_finished:
  282.       /* The file is done being remade.  */
  283.       break;
  284.  
  285.     case cs_running:
  286.     case cs_deps_running:
  287.       /* Don't run the other :: rules for this
  288.          file until this rule is finished.  */
  289.       return 0;
  290.  
  291.     default:
  292.       assert (f->command_state == cs_running);
  293.       break;
  294.     }
  295.     }
  296.  
  297.   return status;
  298. }
  299.  
  300. /* Consider a single `struct file' and update it as appropriate.  */
  301.  
  302. static int
  303. update_file_1 (file, depth)
  304.      struct file *file;
  305.      unsigned int depth;
  306. {
  307.   register time_t this_mtime;
  308.   int noexist, must_make, deps_changed;
  309.   int dep_status = 0;
  310.   register struct dep *d, *lastd;
  311.   int running = 0;
  312.  
  313.   DEBUGPR ("Considering target file `%s'.\n");
  314.  
  315.   if (file->updated)
  316.     {
  317.       if (file->update_status > 0)
  318.     {
  319.       DEBUGPR ("Recently tried and failed to update file `%s'.\n");
  320.       return file->update_status;
  321.     }
  322.  
  323.       DEBUGPR ("File `%s' was considered already.\n");
  324.       return 0;
  325.     }
  326.  
  327.   switch (file->command_state)
  328.     {
  329.     case cs_not_started:
  330.     case cs_deps_running:
  331.       break;
  332.     case cs_running:
  333.       DEBUGPR ("Still updating file `%s'.\n");
  334.       return 0;
  335.     case cs_finished:
  336.       DEBUGPR ("Finished updating file `%s'.\n");
  337.       return file->update_status;
  338.     default:
  339.       abort ();
  340.     }
  341.  
  342.   ++depth;
  343.  
  344.   /* Notice recursive update of the same file.  */
  345.   file->updating = 1;
  346.  
  347.   /* Looking at the file's modtime beforehand allows the possibility
  348.      that its name may be changed by a VPATH search, and thus it may
  349.      not need an implicit rule.  If this were not done, the file
  350.      might get implicit commands that apply to its initial name, only
  351.      to have that name replaced with another found by VPATH search.  */
  352.  
  353.   this_mtime = file_mtime (file);
  354.   check_renamed (file);
  355.   noexist = this_mtime == (time_t) -1;
  356.   if (noexist)
  357.     DEBUGPR ("File `%s' does not exist.\n");
  358.  
  359.   must_make = noexist;
  360.  
  361.   /* If file was specified as a target with no commands,
  362.      come up with some default commands.  */
  363.  
  364.   if (!file->phony && file->cmds == 0 && !file->tried_implicit)
  365.     {
  366.       if (try_implicit_rule (file, depth))
  367.     DEBUGPR ("Found an implicit rule for `%s'.\n");
  368.       else
  369.     DEBUGPR ("No implicit rule found for `%s'.\n");
  370.       file->tried_implicit = 1;
  371.     }
  372.   if (file->cmds == 0 && !file->is_target
  373.       && default_file != 0 && default_file->cmds != 0)
  374.     {
  375.       DEBUGPR ("Using default commands for `%s'.\n");
  376.       file->cmds = default_file->cmds;
  377.     }
  378.  
  379.   /* Update all non-intermediate files we depend on, if necessary,
  380.      and see whether any of them is more recent than this file.  */
  381.  
  382.   lastd = 0;
  383.   d = file->deps;
  384.   while (d != 0)
  385.     {
  386.       time_t mtime;
  387.  
  388.       check_renamed (d->file);
  389.  
  390.       mtime = file_mtime (d->file);
  391.       check_renamed (d->file);
  392.  
  393.       if (d->file->updating)
  394.     {
  395.       error ("Circular %s <- %s dependency dropped.",
  396.          file->name, d->file->name);
  397.       if (lastd == 0)
  398.         {
  399.           file->deps = d->next;
  400.           free ((char *) d);
  401.           d = file->deps;
  402.         }
  403.       else
  404.         {
  405.           lastd->next = d->next;
  406.           free ((char *) d);
  407.           d = lastd->next;
  408.         }
  409.       continue;
  410.     }
  411.  
  412.       d->file->parent = file;
  413.       dep_status |= check_dep (d->file, depth, this_mtime, &must_make);
  414.       check_renamed (d->file);
  415.  
  416.       {
  417.     register struct file *f = d->file;
  418.     if (f->double_colon)
  419.       f = f->double_colon;
  420.     do
  421.       {
  422.         running |= (f->command_state == cs_running
  423.             || f->command_state == cs_deps_running);
  424.         f = f->prev;
  425.       }
  426.     while (f != 0);
  427.       }
  428.  
  429.       if (dep_status != 0 && !keep_going_flag)
  430.     break;
  431.  
  432.       if (!running)
  433.     d->changed = file_mtime (d->file) != mtime;
  434.  
  435.       lastd = d;
  436.       d = d->next;
  437.     }
  438.  
  439.   /* Now we know whether this target needs updating.
  440.      If it does, update all the intermediate files we depend on.  */
  441.  
  442.   if (must_make)
  443.     {
  444.       for (d = file->deps; d != 0; d = d->next)
  445.     if (d->file->intermediate)
  446.       {
  447.         time_t mtime = file_mtime (d->file);
  448.         check_renamed (d->file);
  449.         d->file->parent = file;
  450.         dep_status |= update_file (d->file, depth);
  451.         check_renamed (d->file);
  452.  
  453.         {
  454.           register struct file *f = d->file;
  455.           if (f->double_colon)
  456.         f = f->double_colon;
  457.           do
  458.         {
  459.           running |= (f->command_state == cs_running
  460.                   || f->command_state == cs_deps_running);
  461.           f = f->prev;
  462.         }
  463.           while (f != 0);
  464.         }
  465.  
  466.         if (dep_status != 0 && !keep_going_flag)
  467.           break;
  468.  
  469.         if (!running)
  470.           d->changed = ((file->phony && file->cmds != 0)
  471.                 || file_mtime (d->file) != mtime);
  472.       }
  473.     }
  474.  
  475.   file->updating = 0;
  476.  
  477.   DEBUGPR ("Finished dependencies of target file `%s'.\n");
  478.  
  479.   if (running)
  480.     {
  481.       set_command_state (file, cs_deps_running);
  482.       --depth;
  483.       DEBUGPR ("The dependencies of `%s' are being made.\n");
  484.       return 0;
  485.     }
  486.  
  487.   /* If any dependency failed, give up now.  */
  488.  
  489.   if (dep_status != 0)
  490.     {
  491.       file->update_status = dep_status;
  492.       notice_finished_file (file);
  493.  
  494.       depth--;
  495.  
  496.       DEBUGPR ("Giving up on target file `%s'.\n");
  497.  
  498.       if (depth == 0 && keep_going_flag
  499.       && !just_print_flag && !question_flag)
  500.     error ("Target `%s' not remade because of errors.", file->name);
  501.  
  502.       return dep_status;
  503.     }
  504.  
  505.   if (file->command_state == cs_deps_running)
  506.     /* The commands for some deps were running on the last iteration, but
  507.        they have finished now.  Reset the command_state to not_started to
  508.        simplify later bookkeeping.  It is important that we do this only
  509.        when the prior state was cs_deps_running, because that prior state
  510.        was definitely propagated to FILE's also_make's by set_command_state
  511.        (called above), but in another state an also_make may have
  512.        independently changed to finished state, and we would confuse that
  513.        file's bookkeeping (updated, but not_started is bogus state).  */
  514.     set_command_state (file, cs_not_started);
  515.  
  516.   /* Now record which dependencies are more
  517.      recent than this file, so we can define $?.  */
  518.  
  519.   deps_changed = 0;
  520.   for (d = file->deps; d != 0; d = d->next)
  521.     {
  522.       time_t d_mtime = file_mtime (d->file);
  523.       check_renamed (d->file);
  524.  
  525. #if 1    /* %%% In version 4, remove this code completely to
  526.        implement not remaking deps if their deps are newer
  527.        than their parents.  */
  528.       if (d_mtime == (time_t) -1 && !d->file->intermediate)
  529.     /* We must remake if this dep does not
  530.        exist and is not intermediate.  */
  531.     must_make = 1;
  532. #endif
  533.  
  534.       /* Set DEPS_CHANGED if this dep actually changed.  */
  535.       deps_changed |= d->changed;
  536.  
  537.       /* Set D->changed if either this dep actually changed,
  538.      or its dependent, FILE, is older or does not exist.  */
  539.       d->changed |= noexist || d_mtime > this_mtime;
  540.  
  541.       if (debug_flag && !noexist)
  542.     {
  543.       print_spaces (depth);
  544.       if (d_mtime == (time_t) -1)
  545.         printf ("Dependency `%s' does not exist.\n", dep_name (d));
  546.       else
  547.         printf ("Dependency `%s' is %s than dependent `%s'.\n",
  548.             dep_name (d), d->changed ? "newer" : "older", file->name);
  549.       fflush (stdout);
  550.     }
  551.     }
  552.  
  553.   /* Here depth returns to the value it had when we were called.  */
  554.   depth--;
  555.  
  556.   if (file->double_colon && file->deps == 0)
  557.     {
  558.       must_make = 1;
  559.       DEBUGPR ("Target `%s' is double-colon and has no dependencies.\n");
  560.     }
  561.   else if (!noexist && file->is_target && !deps_changed && file->cmds == 0)
  562.     {
  563.       must_make = 0;
  564.       DEBUGPR ("No commands for `%s' and no dependencies actually changed.\n");
  565.     }
  566.  
  567.   if (!must_make)
  568.     {
  569.       if (debug_flag)
  570.         {
  571.           print_spaces(depth);
  572.           printf("No need to remake target `%s'", file->name);
  573.           if (!streq(file->name, file->hname))
  574.               printf("; using VPATH name `%s'", file->hname);
  575.           printf(".\n");
  576.           fflush(stdout);
  577.         }
  578.  
  579.       notice_finished_file (file);
  580.  
  581.       /* Since we don't need to remake the file, convert it to use the
  582.          VPATH filename if we found one.  hfile will be either the
  583.          local name if no VPATH or the VPATH name if one was found.  */
  584.  
  585.       while (file)
  586.         {
  587.           file->name = file->hname;
  588.           file = file->prev;
  589.         }
  590.  
  591.       return 0;
  592.     }
  593.  
  594.   DEBUGPR ("Must remake target `%s'.\n");
  595.  
  596.   /* It needs to be remade.  If it's VPATH and not reset via GPATH, toss the
  597.      VPATH */
  598.   if (!streq(file->name, file->hname))
  599.     {
  600.       if (debug_flag)
  601.         {
  602.           print_spaces (depth);
  603.           printf("  Ignoring VPATH name `%s'.\n", file->hname);
  604.           fflush(stdout);
  605.         }
  606.       file->ignore_vpath = 1;
  607.     }
  608.  
  609.   /* Now, take appropriate actions to remake the file.  */
  610.   remake_file (file);
  611.  
  612.   if (file->command_state != cs_finished)
  613.     {
  614.       DEBUGPR ("Commands of `%s' are being run.\n");
  615.       return 0;
  616.     }
  617.  
  618.   switch (file->update_status)
  619.     {
  620.     case 2:
  621.       DEBUGPR ("Failed to remake target file `%s'.\n");
  622.       break;
  623.     case 0:
  624.       DEBUGPR ("Successfully remade target file `%s'.\n");
  625.       break;
  626.     case 1:
  627.       DEBUGPR ("Target file `%s' needs remade under -q.\n");
  628.       break;
  629.     default:
  630.       assert (file->update_status >= 0 && file->update_status <= 2);
  631.       break;
  632.     }
  633.  
  634.   file->updated = 1;
  635.   return file->update_status;
  636. }
  637.  
  638. /* Set FILE's `updated' flag and re-check its mtime and the mtime's of all
  639.    files listed in its `also_make' member.  Under -t, this function also
  640.    touches FILE.
  641.  
  642.    On return, FILE->update_status will no longer be -1 if it was.  */
  643.  
  644. void
  645. notice_finished_file (file)
  646.      register struct file *file;
  647. {
  648.   struct dep *d;
  649.   int ran = file->command_state == cs_running;
  650.  
  651.   file->command_state = cs_finished;
  652.   file->updated = 1;
  653.  
  654.   if (touch_flag
  655.       /* The update status will be:
  656.          -1    if this target was not remade;
  657.         0    if 0 or more commands (+ or ${MAKE}) were run and won;
  658.         1    if some commands were run and lost.
  659.      We touch the target if it has commands which either were not run
  660.      or won when they ran (i.e. status is 0).  */
  661.       && file->update_status == 0)
  662.     {
  663.       if (file->cmds != 0 && file->cmds->any_recurse)
  664.     {
  665.       /* If all the command lines were recursive,
  666.          we don't want to do the touching.  */
  667.       unsigned int i;
  668.       for (i = 0; i < file->cmds->ncommand_lines; ++i)
  669.         if (!(file->cmds->lines_flags[i] & COMMANDS_RECURSE))
  670.           goto have_nonrecursing;
  671.     }
  672.       else
  673.     {
  674.     have_nonrecursing:
  675.       if (file->phony)
  676.         file->update_status = 0;
  677.       else
  678.         /* Should set file's modification date and do nothing else.  */
  679.         file->update_status = touch_file (file);
  680.     }
  681.     }
  682.  
  683.   if (ran && !file->phony)
  684.     {
  685.       struct file *f;
  686.  
  687.       if (just_print_flag || question_flag
  688.       || (file->is_target && file->cmds == 0))
  689.     file->last_mtime = NEW_MTIME;
  690.       else
  691.     file->last_mtime = 0;
  692.  
  693.       /* Propagate the change of modification time to all the double-colon
  694.      entries for this file.  */
  695.       for (f = file->double_colon; f != 0; f = f->next)
  696.     f->last_mtime = file->last_mtime;
  697.     }
  698.  
  699.   if (ran && file->update_status != -1)
  700.     /* We actually tried to update FILE, which has
  701.        updated its also_make's as well (if it worked).
  702.        If it didn't work, it wouldn't work again for them.
  703.        So mark them as updated with the same status.  */
  704.     for (d = file->also_make; d != 0; d = d->next)
  705.       {
  706.     d->file->command_state = cs_finished;
  707.     d->file->updated = 1;
  708.     d->file->update_status = file->update_status;
  709.  
  710.     if (ran && !d->file->phony)
  711.       /* Fetch the new modification time.
  712.          We do this instead of just invalidating the cached time
  713.          so that a vpath_search can happen.  Otherwise, it would
  714.          never be done because the target is already updated.  */
  715.       (void) f_mtime (d->file, 0);
  716.       }
  717.   else if (file->update_status == -1)
  718.     /* Nothing was done for FILE, but it needed nothing done.
  719.        So mark it now as "succeeded".  */
  720.     file->update_status = 0;
  721. }
  722.  
  723. /* Check whether another file (whose mtime is THIS_MTIME)
  724.    needs updating on account of a dependency which is file FILE.
  725.    If it does, store 1 in *MUST_MAKE_PTR.
  726.    In the process, update any non-intermediate files
  727.    that FILE depends on (including FILE itself).
  728.    Return nonzero if any updating failed.  */
  729.  
  730. static int
  731. check_dep (file, depth, this_mtime, must_make_ptr)
  732.      struct file *file;
  733.      unsigned int depth;
  734.      time_t this_mtime;
  735.      int *must_make_ptr;
  736. {
  737.   register struct dep *d;
  738.   int dep_status = 0;
  739.  
  740.   ++depth;
  741.   file->updating = 1;
  742.  
  743.   if (!file->intermediate)
  744.     /* If this is a non-intermediate file, update it and record
  745.        whether it is newer than THIS_MTIME.  */
  746.     {
  747.       time_t mtime;
  748.       dep_status = update_file (file, depth);
  749.       check_renamed (file);
  750.       mtime = file_mtime (file);
  751.       check_renamed (file);
  752.       if (mtime == (time_t) -1 || mtime > this_mtime)
  753.     *must_make_ptr = 1;
  754.     }
  755.   else
  756.     {
  757.       /* FILE is an intermediate file.  */
  758.       time_t mtime;
  759.  
  760.       if (!file->phony && file->cmds == 0 && !file->tried_implicit
  761.       && file->secondary)
  762.     {
  763.       if (try_implicit_rule (file, depth))
  764.         DEBUGPR ("Found an implicit rule for `%s'.\n");
  765.       else
  766.         DEBUGPR ("No implicit rule found for `%s'.\n");
  767.       file->tried_implicit = 1;
  768.     }
  769.       if (file->cmds == 0 && !file->is_target && file->secondary
  770.       && default_file != 0 && default_file->cmds != 0)
  771.     {
  772.       DEBUGPR ("Using default commands for `%s'.\n");
  773.       file->cmds = default_file->cmds;
  774.     }
  775.  
  776.       /* If the intermediate file actually exists
  777.      and is newer, then we should remake from it.  */
  778.       check_renamed (file);
  779.       mtime = file_mtime (file);
  780.       check_renamed (file);
  781.       if (mtime != (time_t) -1 && mtime > this_mtime)
  782.     *must_make_ptr = 1;
  783.       /* Otherwise, update all non-intermediate files we depend on,
  784.          if necessary, and see whether any of them is more
  785.          recent than the file on whose behalf we are checking.  */
  786.       else
  787.     {
  788.       register struct dep *lastd;
  789.  
  790.       lastd = 0;
  791.       d = file->deps;
  792.       while (d != 0)
  793.         {
  794.           if (d->file->updating)
  795.         {
  796.           error ("Circular %s <- %s dependency dropped.",
  797.              file->name, d->file->name);
  798.           if (lastd == 0)
  799.             {
  800.               file->deps = d->next;
  801.               free ((char *) d);
  802.               d = file->deps;
  803.             }
  804.           else
  805.             {
  806.               lastd->next = d->next;
  807.               free ((char *) d);
  808.               d = lastd->next;
  809.             }
  810.           continue;
  811.         }
  812.  
  813.           d->file->parent = file;
  814.           dep_status |= check_dep (d->file, depth, this_mtime, must_make_ptr);
  815.           check_renamed (d->file);
  816.           if (dep_status != 0 && !keep_going_flag)
  817.         break;
  818.  
  819.           if (d->file->command_state == cs_running
  820.           || d->file->command_state == cs_deps_running)
  821.         /* Record that some of FILE's dependencies are still being made.
  822.            This tells the upper levels to wait on processing it until
  823.            the commands are finished.  */
  824.         set_command_state (file, cs_deps_running);
  825.  
  826.           lastd = d;
  827.           d = d->next;
  828.         }
  829.     }
  830.     }
  831.  
  832.   file->updating = 0;
  833.   return dep_status;
  834. }
  835.  
  836. /* Touch FILE.  Return zero if successful, one if not.  */
  837.  
  838. #define TOUCH_ERROR(call) return (perror_with_name (call, file->name), 1)
  839.  
  840. static int
  841. touch_file (file)
  842.      register struct file *file;
  843. {
  844.   if (!silent_flag)
  845.     message (0, "touch %s", file->name);
  846.  
  847. #ifndef    NO_ARCHIVES
  848.   if (ar_name (file->name))
  849.     return ar_touch (file->name);
  850.   else
  851. #endif
  852.     {
  853.       int fd = open (file->name, O_RDWR | O_CREAT, 0666);
  854.  
  855.       if (fd < 0)
  856.     TOUCH_ERROR ("touch: open: ");
  857.       else
  858.     {
  859.       struct stat statbuf;
  860.       char buf;
  861.       int status;
  862.  
  863. #ifdef EINTR
  864.       do
  865. #endif
  866.         status = fstat (fd, &statbuf);
  867. #ifdef EINTR
  868.       while (status < 0 && errno == EINTR);
  869. #endif
  870.       if (status < 0)
  871.         TOUCH_ERROR ("touch: fstat: ");
  872.       /* Rewrite character 0 same as it already is.  */
  873.       if (read (fd, &buf, 1) < 0)
  874.         TOUCH_ERROR ("touch: read: ");
  875.       if (lseek (fd, 0L, 0) < 0L)
  876.         TOUCH_ERROR ("touch: lseek: ");
  877.       if (write (fd, &buf, 1) < 0)
  878.         TOUCH_ERROR ("touch: write: ");
  879.       /* If file length was 0, we just
  880.          changed it, so change it back.  */
  881.       if (statbuf.st_size == 0)
  882.         {
  883.           (void) close (fd);
  884.           fd = open (file->name, O_RDWR | O_TRUNC, 0666);
  885.           if (fd < 0)
  886.         TOUCH_ERROR ("touch: open: ");
  887.         }
  888.       (void) close (fd);
  889.     }
  890.     }
  891.  
  892.   return 0;
  893. }
  894.  
  895. /* Having checked and updated the dependencies of FILE,
  896.    do whatever is appropriate to remake FILE itself.
  897.    Return the status from executing FILE's commands.  */
  898.  
  899. static void
  900. remake_file (file)
  901.      struct file *file;
  902. {
  903.   if (file->cmds == 0)
  904.     {
  905.       if (file->phony)
  906.     /* Phony target.  Pretend it succeeded.  */
  907.     file->update_status = 0;
  908.       else if (file->is_target)
  909.     /* This is a nonexistent target file we cannot make.
  910.        Pretend it was successfully remade.  */
  911.     file->update_status = 0;
  912.       else
  913.     {
  914.       /* This is a dependency file we cannot remake.  Fail.  */
  915.       static const char msg_noparent[]
  916.         = "%sNo rule to make target `%s'%s";
  917.       static const char msg_parent[]
  918.         = "%sNo rule to make target `%s', needed by `%s'%s";
  919.       if (keep_going_flag || file->dontcare)
  920.         {
  921.           if (!file->dontcare)
  922.         {
  923.           if (file->parent == 0)
  924.             error (msg_noparent, "*** ", file->name, ".");
  925.           else
  926.             error (msg_parent, "*** ",
  927.                file->name, file->parent->name, ".");
  928.         }
  929.            file->update_status = 2;
  930.         }
  931.       else
  932.         {
  933.           if (file->parent == 0)
  934.         fatal (msg_noparent, "", file->name, "");
  935.           else
  936.         fatal (msg_parent, "", file->name, file->parent->name, "");
  937.         }
  938.     }
  939.     }
  940.   else
  941.     {
  942.       chop_commands (file->cmds);
  943.  
  944.       if (!touch_flag || file->cmds->any_recurse)
  945.     {
  946.       execute_file_commands (file);
  947.       return;
  948.     }
  949.       else
  950.     /* This tells notice_finished_file it is ok to touch the file.  */
  951.     file->update_status = 0;
  952.     }
  953.  
  954.   /* This does the touching under -t.  */
  955.   notice_finished_file (file);
  956. }
  957.  
  958. /* Return the mtime of a file, given a `struct file'.
  959.    Caches the time in the struct file to avoid excess stat calls.
  960.  
  961.    If the file is not found, and SEARCH is nonzero, VPATH searching and
  962.    replacement is done.  If that fails, a library (-lLIBNAME) is tried and
  963.    the library's actual name (/lib/libLIBNAME.a, etc.) is substituted into
  964.    FILE.  */
  965.  
  966. time_t
  967. f_mtime (file, search)
  968.      register struct file *file;
  969.      int search;
  970. {
  971.   time_t mtime;
  972.  
  973.   /* File's mtime is not known; must get it from the system.  */
  974.  
  975. #ifndef    NO_ARCHIVES
  976.   if (ar_name (file->name))
  977.     {
  978.       /* This file is an archive-member reference.  */
  979.  
  980.       char *arname, *memname;
  981.       struct file *arfile;
  982.       int arname_used = 0;
  983.  
  984.       /* Find the archive's name.  */
  985.       ar_parse_name (file->name, &arname, &memname);
  986.  
  987.       /* Find the modification time of the archive itself.
  988.      Also allow for its name to be changed via VPATH search.  */
  989.       arfile = lookup_file (arname);
  990.       if (arfile == 0)
  991.     {
  992.       arfile = enter_file (arname);
  993.       arname_used = 1;
  994.     }
  995.       mtime = f_mtime (arfile, search);
  996.       check_renamed (arfile);
  997.       if (search && strcmp (arfile->name, arname))
  998.     {
  999.       /* The archive's name has changed.
  1000.          Change the archive-member reference accordingly.  */
  1001.  
  1002.       unsigned int arlen, memlen;
  1003.  
  1004.       if (!arname_used)
  1005.         {
  1006.           free (arname);
  1007.           arname_used = 1;
  1008.         }
  1009.  
  1010.       arname = arfile->name;
  1011.       arlen = strlen (arname);
  1012.       memlen = strlen (memname);
  1013.  
  1014.       free (file->name);
  1015.  
  1016.       file->name = (char *) xmalloc (arlen + 1 + memlen + 2);
  1017.       bcopy (arname, file->name, arlen);
  1018.       file->name[arlen] = '(';
  1019.       bcopy (memname, file->name + arlen + 1, memlen);
  1020.       file->name[arlen + 1 + memlen] = ')';
  1021.       file->name[arlen + 1 + memlen + 1] = '\0';
  1022.     }
  1023.  
  1024.       if (!arname_used)
  1025.     free (arname);
  1026.       free (memname);
  1027.  
  1028.       if (mtime == (time_t) -1)
  1029.     /* The archive doesn't exist, so it's members don't exist either.  */
  1030.     return (time_t) -1;
  1031.  
  1032.       mtime = ar_member_date (file->name);
  1033.     }
  1034.   else
  1035. #endif
  1036.     {
  1037.       mtime = name_mtime (file->name);
  1038.  
  1039.       if (mtime == (time_t) -1 && search && !file->ignore_vpath)
  1040.     {
  1041.       /* If name_mtime failed, search VPATH.  */
  1042.       char *name = file->name;
  1043.       if (vpath_search (&name, &mtime)
  1044.           /* Last resort, is it a library (-lxxx)?  */
  1045.           || (name[0] == '-' && name[1] == 'l'
  1046.           && library_search (&name, &mtime)))
  1047.         {
  1048.           if (mtime != 0)
  1049.         /* vpath_search and library_search store zero in MTIME
  1050.            if they didn't need to do a stat call for their work.  */
  1051.         file->last_mtime = mtime;
  1052.  
  1053.               /* If we found it in VPATH, see if it's in GPATH too; if so,
  1054.                  change the name right now; if not, defer until after the
  1055.                  dependencies are updated. */
  1056.               if (gpath_search (name, strlen(name) - strlen(file->name) - 1))
  1057.                 {
  1058.                   rename_file (file, name);
  1059.                   check_renamed (file);
  1060.                   return file_mtime (file);
  1061.                 }
  1062.  
  1063.           rehash_file (file, name);
  1064.           check_renamed (file);
  1065.           mtime = name_mtime (name);
  1066.         }
  1067.     }
  1068.     }
  1069.  
  1070.   {
  1071.     /* Files can have bogus timestamps that nothing newly made will be
  1072.        "newer" than.  Updating their dependents could just result in loops.
  1073.        So notify the user of the anomaly with a warning.
  1074.  
  1075.        We only need to do this once, for now. */
  1076.  
  1077.     static time_t now = 0;
  1078.     if (!clock_skew_detected
  1079.         && mtime != (time_t)-1 && mtime > now
  1080.         && !file->updated)
  1081.       {
  1082.     /* This file's time appears to be in the future.
  1083.        Update our concept of the present, and compare again.  */
  1084.  
  1085.     extern time_t time ();
  1086.     time (&now);
  1087.  
  1088. #ifdef WINDOWS32
  1089.     /*
  1090.      * FAT filesystems round time to nearest even second(!). Just
  1091.      * allow for any file (NTFS or FAT) to perhaps suffer from this
  1092.      * braindamage.
  1093.      *
  1094.      * Apparently, this doesn't happen with the MS-DOS/DJGPP port,
  1095.      * although MS-DOS and MS-Windows 3.X/9X also use FAT filesystems.
  1096.      */
  1097.     if (mtime > now && (((mtime % 2) == 0) && ((mtime-1) > now)))
  1098. #else
  1099.         if (mtime > now)
  1100. #endif
  1101.           {
  1102.             error("*** Warning: File `%s' has modification time in the future",
  1103.                   file->name);
  1104.             clock_skew_detected = 1;
  1105.           }
  1106.       }
  1107.   }
  1108.  
  1109.   /* Store the mtime into all the entries for this file.  */
  1110.   if (file->double_colon)
  1111.     file = file->double_colon;
  1112.   do
  1113.     {
  1114.       file->last_mtime = mtime;
  1115.       file = file->prev;
  1116.     } while (file != 0);
  1117.  
  1118.   return mtime;
  1119. }
  1120.  
  1121.  
  1122. /* Return the mtime of the file or archive-member reference NAME.  */
  1123.  
  1124. static time_t
  1125. name_mtime (name)
  1126.      register char *name;
  1127. {
  1128.   struct stat st;
  1129.  
  1130.   if (stat (name, &st) < 0)
  1131.     return (time_t) -1;
  1132.  
  1133.   return (time_t) st.st_mtime;
  1134. }
  1135.  
  1136.  
  1137. /* Search for a library file specified as -lLIBNAME, searching for a
  1138.    suitable library file in the system library directories and the VPATH
  1139.    directories.  */
  1140.  
  1141. static int
  1142. library_search (lib, mtime_ptr)
  1143.      char **lib;
  1144.      time_t *mtime_ptr;
  1145. {
  1146.   static char *dirs[] =
  1147.     {
  1148. #ifndef _AMIGA
  1149.       "/lib",
  1150.       "/usr/lib",
  1151. #endif
  1152. #if defined(WINDOWS32) && !defined(LIBDIR)
  1153. /*
  1154.  * This is completely up to the user at product install time. Just define
  1155.  * a placeholder.
  1156.  */
  1157. #define LIBDIR "."
  1158. #endif
  1159.       LIBDIR,            /* Defined by configuration.  */
  1160.       0
  1161.     };
  1162.  
  1163.   char *libname = &(*lib)[2];    /* Name without the `-l'.  */
  1164.   time_t mtime;
  1165.  
  1166.   /* Buffer to construct possible names in.  */
  1167.   char *buf = xmalloc (sizeof (LIBDIR) + 8 + strlen (libname) + 4 + 2 + 1);
  1168.   char *file, **dp;
  1169.  
  1170.   /* Look first for `libNAME.a' in the current directory.  */
  1171.  
  1172. #ifndef _AMIGA
  1173.   sprintf (buf, "lib%s.a", libname);
  1174. #else
  1175.   sprintf (buf, "%s.lib", libname);
  1176. #endif
  1177.   mtime = name_mtime (buf);
  1178.   if (mtime != (time_t) -1)
  1179.     {
  1180.       *lib = buf;
  1181.       if (mtime_ptr != 0)
  1182.     *mtime_ptr = mtime;
  1183.       return 1;
  1184.     }
  1185.  
  1186.   /* Now try VPATH search on that.  */
  1187.  
  1188.   file = buf;
  1189.   if (vpath_search (&file, mtime_ptr))
  1190.     {
  1191.       free (buf);
  1192.       *lib = file;
  1193.       return 1;
  1194.     }
  1195.  
  1196.   /* Now try the standard set of directories.  */
  1197.  
  1198. #ifdef  __MSDOS__
  1199.   {
  1200.     /* The default library directory is at ${DJDIR}/lib.  */
  1201.     struct variable *djdir = lookup_variable ("DJDIR", 5);
  1202.  
  1203.     if (djdir)
  1204.       {
  1205.     size_t djdir_len = strlen (djdir->value);
  1206.  
  1207.     if (djdir_len > sizeof(LIBDIR) + 8 + strlen(libname) + 4 + 2)
  1208.       buf = (char *) xrealloc (djdir_len + 1);
  1209.     sprintf (buf, "%s/lib/lib%s.a", djdir->value, libname);
  1210.     mtime = name_mtime (buf);
  1211.     if (mtime != (time_t) -1)
  1212.       {
  1213.         *lib = buf;
  1214.         if (mtime_ptr != 0)
  1215.           *mtime_ptr = mtime;
  1216.         return 1;
  1217.       }
  1218.       }
  1219.   }
  1220. #endif
  1221.  
  1222.   for (dp = dirs; *dp != 0; ++dp)
  1223.     {
  1224. #ifndef _AMIGA
  1225.       sprintf (buf, "%s/lib%s.a", *dp, libname);
  1226. #else
  1227.       sprintf (buf, "%s/%s.lib", *dp, libname);
  1228. #endif
  1229.       mtime = name_mtime (buf);
  1230.       if (mtime != (time_t) -1)
  1231.     {
  1232.       *lib = buf;
  1233.       if (mtime_ptr != 0)
  1234.         *mtime_ptr = mtime;
  1235.       return 1;
  1236.     }
  1237.     }
  1238.  
  1239.   free (buf);
  1240.   return 0;
  1241. }
  1242.